package com.swt.gap.controller;

import com.swt.gap.common.PageResult;
import com.swt.gap.model.entity.TaskResultWithBLOBs;
import com.swt.gap.model.graphmodel.UpdateTaskResultContentModel;
import com.swt.gap.model.vo.TaskResultUpdateVO;
import com.swt.gap.model.vo.TaskVO;
import com.swt.gap.model.vo.taskresult.AnalysisResultVO;
import com.swt.gap.model.vo.taskresult.TaskResultListVO;
import com.swt.gap.service.TaskResultService;
import com.swt.gap.util.UserLoginUtils;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author tyh
 * @date 2020/5/8 15:31
 */


@Api(tags = "301 任务分析结果 v2")
@RestController
@RequestMapping("/task-result")
@Slf4j
public class TaskResultController {

    private final TaskResultService taskResultService;

    public TaskResultController(TaskResultService taskResultService) {
        this.taskResultService = taskResultService;
    }

    @PostMapping
    @ApiOperation(value = "创建分析结果", notes = "创建分析结果")
    @ApiImplicitParam(name = "taskResult", dataType = "TaskResultWithBLOBs",paramType = "body", value = "分析结果信息")
    @ApiResponses({@ApiResponse(code = 201, message = "创建成功")})
    public ResponseEntity addTaskResult(@RequestBody TaskResultWithBLOBs taskResult){
        //对分析结果名的校验
        String resultName = taskResult.getResultName();
        if(StringUtils.isNotBlank(resultName)){
            resultName = resultName.trim();
            if(resultName.length() > 20){
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("名称长度不能超过20");
            }
            taskResult.setResultName(resultName);
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("名称不能为空");
        }

        Integer integer = taskResultService.addTaskResult(taskResult);
        if (integer < 1){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("保存分析结果失败！");
        }

        return ResponseEntity.status(HttpStatus.CREATED).body(taskResult);
    }


    @PutMapping("/{resultId}")
    @ApiOperation(value = "编辑分析结果名称和描述", notes = "编辑分析结果名称和描述")
    @ApiImplicitParams ({
            @ApiImplicitParam(name = "resultId", dataType = "int",paramType = "path", value = "分析结果Id"),
            @ApiImplicitParam(name = "taskResultInfo", dataType = "TaskResultUpdateVO",paramType = "body", value = "分析结果名称和备注")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    public ResponseEntity updateTaskResult(@RequestBody TaskResultUpdateVO taskResultInfo, @PathVariable Integer resultId){
        //对分析结果名的校验
        String resultName = taskResultInfo.getResultName();
        if(StringUtils.isNotBlank(resultName)){
            resultName = resultName.trim();
            if(resultName.length() > 20){
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("名称长度不能超过20");
            }
            taskResultInfo.setResultName(resultName);
        }else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("名称不能为空");
        }

        taskResultInfo.setResultId(resultId);
        taskResultService.updateTaskResultInfo(taskResultInfo);
        return ResponseEntity.ok(taskResultInfo);
    }

    @PutMapping("/update-content")
    @ApiOperation(value = "更新分析结果内容", notes = "更新分析结果内容")
    @ApiImplicitParam(name = "updateTaskResultContentModel", dataType = "UpdateTaskResultContentModel",paramType = "body", value = "更新任务分析结果内容")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    public ResponseEntity updateTaskResultContent(@RequestBody UpdateTaskResultContentModel updateTaskResultContentModel){
        return ResponseEntity.ok(taskResultService.updateTaskResultContent(updateTaskResultContentModel));
    }


    @DeleteMapping("/{resultId}")
    @ApiOperation(value = "删除分析结果", notes = "删除分析结果")
    @ApiImplicitParam(name = "resultId", dataType = "int",paramType = "path", value = "分析结果Id")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    public ResponseEntity deleteTaskResult(@PathVariable Integer resultId){
        taskResultService.deleteTaskResult(resultId);
        return ResponseEntity.ok("删除成功");
    }


    @GetMapping
    @ApiOperation(value = "根据任务id获取该任务的分析结果列表", notes = "根据任务id获取该任务的分析结果列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "taskId", dataType = "int", value = "任务id"),
            @ApiImplicitParam(paramType = "query", name = "keyword", dataType = "String", value = "输入的搜索条件, 无搜索时不传此参数"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", dataType = "int", value = "当前页"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", dataType = "int", value = "每页的条数")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = PageResult.class)})
    public ResponseEntity listTaskResult(@RequestParam Integer taskId, @RequestParam String keyword,
                                       @RequestParam Integer pageNum, @RequestParam Integer pageSize){

        return ResponseEntity.ok(taskResultService.getTaskResultByKeyword(taskId, keyword, pageNum, pageSize));
    }

    @GetMapping("/{resultId}")
    @ApiOperation(value = "查看分析结果", notes = "查看分析结果" )
    @ApiImplicitParam(name = "resultId", dataType = "Integer",paramType = "path", value = "分析结果Id")
    @ApiResponse(code = 200, message = "ok")
    public ResponseEntity viewTaskResult(@PathVariable Integer resultId){
        return ResponseEntity.ok(taskResultService.viewTaskResult(resultId));
    }


    /*@GetMapping("/view/{taskId}")
    @ApiOperation(value = "查看指定任务的分析结果列表", notes = "查看指定任务的分析结果列表" )
    @ApiImplicitParam(name = "taskId", dataType = "int",paramType = "path", value = "任务Id")
    @ApiResponse(code = 200, message = "ok", response = TaskResultListVO.class)
    public ResponseEntity getTaskResultList(@PathVariable Integer taskId){
        return ResponseEntity.ok(taskResultService.getTaskResultList(taskId));
    }
*/

    @GetMapping("/access-result")
    @ApiOperation(value = "获取可查看的任务以及分析结果列表", notes = "获取可查看的任务以及分析结果列表, 按任务分组")

    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = TaskResultListVO.class, responseContainer = "List")})
    public ResponseEntity<List<TaskResultListVO>> listAccessTaskResult(){

        return ResponseEntity.ok(taskResultService.listAccessTaskResult());
    }

    @GetMapping("/access-task-result")
    @ApiOperation(value = "获取登录用户可查看的所有分析结果", notes = "获取登录用户可查看的所有分析结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", dataType = "String", value = "输入的搜索条件, 无搜索时不传此参数"),
            @ApiImplicitParam(name = "pageNum", dataType = "int", value = "获取的当前页码，默认从第一页开始"),
            @ApiImplicitParam(name = "pageSize", dataType = "int", value = "每页的条数"),
    })
    @ApiResponse(code = 200, message = "ok", response = PageResult.class)
    public ResponseEntity<PageResult<AnalysisResultVO>> listAccessResult(@RequestParam(required = false) String keyword,
                                         @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        int userId = UserLoginUtils.getUser().getUserId();
        return ResponseEntity.ok(taskResultService.listAccessResult(pageNum,pageSize,keyword,userId));
    }
}
